import { setElementVisible } from '../utils/index'
import ModeBoard from './ModeBoard'

type BtnName =
  | 'startBtnEle'
  | 'suspendBtnEle'
  | 'continueBtnEle'
  | 'restartBtnEle'
class ButtonBoard {
  private startBtnEle: HTMLElement
  private restartBtnEle: HTMLElement
  private suspendBtnEle: HTMLElement
  private continueBtnEle: HTMLElement

  constructor() {
    this.startBtnEle = document.getElementById('btn-start')!
    this.restartBtnEle = document.getElementById('btn-restart')!
    this.suspendBtnEle = document.getElementById('btn-suspend')!
    this.continueBtnEle = document.getElementById('btn-continue')!
    this.resetStatus()
  }

  /**
   * 设置按钮可见性
   * @param BtnName
   * @param isShow
   */
  public setBtnVisible(btnName: BtnName, isShow: boolean) {
    // 进阶和困难模式下,不显示暂停和继续按钮
    if (
      ModeBoard.gameMode != 'simple' &&
      btnName != 'startBtnEle' &&
      btnName != 'restartBtnEle'
    ) {
      return
    }
    setElementVisible(this[btnName], isShow)
  }

  /**
   * 为按钮添加点击事件
   * @param btnName
   * @param handler
   */
  public addEventHandlerForBtn(btnName: BtnName, handler: (ev: Event) => void) {
    this[btnName].addEventListener('click', handler)
  }

  /**
   * 重设状态
   */
  public resetStatus() {
    this.setBtnVisible('continueBtnEle', false)
    this.setBtnVisible('suspendBtnEle', false)
    this.setBtnVisible('restartBtnEle', false)
    this.setBtnVisible('startBtnEle', true)
  }
}

export default new ButtonBoard()
